package com.shop.wk.controller;

import com.shop.wk.common.constant.HttpCode;
import com.shop.wk.common.dto.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.TokenService;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 日月星辰
 * @date 2020/12/25 21:41
 * @description
 */
@Api(value = "权限相关", tags = "PermissionApi")
@Slf4j
@RestController
@RequestMapping("/auth")
public class PermissionController {

	@Autowired
	private ResourceServerTokenServices resourceServerTokenServices;

	@ApiOperation(value = "检查token是否有效", notes = "有效code=0 无效code=1  data为有效时间/秒")
	@GetMapping("/checkToken")
	public ResponseResult checkToken(@RequestParam("token") String token) {
		OAuth2AccessToken accessToken = resourceServerTokenServices.readAccessToken(token);
		try {
			Thread.sleep(12000);
		}
		catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (accessToken != null) {
			log.info(accessToken.toString());
			log.info(accessToken.getValue());
			return new ResponseResult(HttpCode.OK, "ok", accessToken.getExpiresIn());
		}
		return new ResponseResult(HttpCode.FAIL, "no");
	}

	// @PostMapping("/checkPermission")
	// public Boolean checkPermission(@RequestBody PermissionDTO permissionDTO) {
	// return permissionService.checkPermission(permissionDTO);
	// }

}
